package com.xydtech.ecard.busi.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportCard;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportConfig;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportCardService;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportConfigService;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 电子名片业务填报绑卡类信息Controller
 *
 * @author yuzhiguo
 * @date 2024-09-26 上午9:12
 */
@Controller
@Slf4j
@Api(tags = "电子名片-业务填报绑卡类接口")
@RequestMapping("/ecardSaasApi/busi/reportCard")
public class EcardApiBusiReportCardController extends EcardApiBaseController {

    @Autowired
    private IEcardApiBusiReportCardService ecardBusiReportCardService;
    @Autowired
    private IEcardApiBusiReportConfigService ecardBusiReportConfigService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;

    /**
     * 电子名片-保存业务填报绑卡类数据接口
     *
     * @author yuzhiguo
     * @date 2024/9/26 上午9:25
     */
    @PostMapping(value = "/addReportCardInfo")
    @ApiOperation(value = "电子名片-保存业务填报绑卡类数据接口", notes = "电子名片-保存业务填报绑卡类数据接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addReportCardInfo(@RequestBody EcardApiBusiReportCard ecardBusiReportCard) {
        try {
            String salt = getSalt();//盐值
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(ecardBusiReportCard.getDeptId());
            //根据业务类型id查询业务类型信息
            String configIds = ecardBusiReportCard.getConfigId();
            for (String configId : configIds.split(",")) {
                EcardApiBusiReportConfig reportConfig = ecardBusiReportConfigService.getReportConfigById(Long.valueOf(configId));
                if ("F".equals(reportConfig.getIsMore())){//是否可以多次保存，否的话根据身份证和业务类型id校验是否已存在
                    EcardApiBusiReportCard reportCard = new EcardApiBusiReportCard();
                    if (!StringUtils.objIsNullOrEmpty(reportCard.getPerCustIdcard())) {
                        //数据加密（手机号、身份证号）
                        reportCard.setPerCustIdcard(AESEncryptionUtils.encrypt(reportCard.getPerCustIdcard(), salt));
                    }
                    reportCard.setDeptId(dept.getDeptId());
                    reportCard.setConfigId(configId);
                    List<EcardApiBusiReportCard> busiReportCardList = ecardBusiReportCardService.getMyReportCardList(reportCard, salt);
                    if (busiReportCardList.size() > 0){
                        return ResponseModel.error("该客户已存在同类型业务！");
                    }
                }
            }
            //管户人id
            ecardBusiReportCard.setManagementPersonnelId(ecardBusiReportCard.getCreatorId());
            int id = ecardBusiReportCardService.addReportCardInfo(ecardBusiReportCard, salt);
            return id > 0 ? ResponseModel.ok(ecardBusiReportCard.getId()) : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询已办理的绑卡类业务列表
     *
     * @author yuzhiguo
     * @date 2024/9/26 上午10:18
     */
    @PostMapping(value = "/getReportCardConfigListByCond")
    @ApiOperation(value = "电子名片-条件查询已办理的绑卡类业务列表", notes = "电子名片-条件查询已办理的绑卡类业务列表")
    @ResponseBody
    @RsaRequest
    public ResponseModel getReportCardConfigListByCond(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            JSONObject jsonObject = JSONObject.parseObject(json);
            String perCustIdcard = jsonObject.getString("perCustIdcard");//身份证号
            if (StringUtils.objIsNullOrEmpty(currentUserId) || StringUtils.objIsNullOrEmpty(perCustIdcard)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            EcardApiBusiReportCard ecardBusiReportCard = new EcardApiBusiReportCard();
            if (!StringUtils.objIsNullOrEmpty(perCustIdcard)) {
                //数据加密（手机号、身份证号）
                ecardBusiReportCard.setPerCustIdcard(AESEncryptionUtils.encrypt(perCustIdcard, salt));
            }
            ecardBusiReportCard.setDeptId(dept.getDeptId());
            List<EcardApiBusiReportConfig> busiReportConfigList = ecardBusiReportCardService.getReportCardConfigListByCond(ecardBusiReportCard);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(busiReportConfigList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询绑卡类业务列表（暂时不用）
     *
     * @author yuzhiguo
     * @date 2024/9/26 上午9:55
     */
    @PostMapping(value = "/getMyReportCardList")
    @ApiOperation(value = "电子名片-条件查询绑卡类业务列表", notes = "电子名片-条件查询绑卡类业务列表")
    @ResponseBody
    @RsaRequest
    public ResponseModel getMyReportCardList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            String perCustIdcard = jsonObject.getString("perCustIdcard");//身份证号
            EcardApiBusiReportCard ecardBusiReportCard = new EcardApiBusiReportCard();
            if (!StringUtils.objIsNullOrEmpty(perCustIdcard)) {
                //数据加密（手机号、身份证号）
                ecardBusiReportCard.setPerCustIdcard(AESEncryptionUtils.encrypt(perCustIdcard, salt));
            }
            ecardBusiReportCard.setManagementPersonnelId(Long.valueOf(currentUserId));
            List<EcardApiBusiReportCard> busiReportCardList = ecardBusiReportCardService.getMyReportCardList(ecardBusiReportCard, salt);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(busiReportCardList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }
}
